DXL function for sibling position

Is there any function to determine the position of an object among its siblings?

e.g. to avoid counting the number of objects in between the object and its parent:

Object o = current
Object sibling
int position = 0

for sibling in parent(current Object) do {
    position ++ // count objects until self is reached
    if (sibling == o) break
}

print position

...or parsing out the last digit of the object number:

Regexp last_digit = regexp2 "[0-9]+$"
Object o = current
int position = 0

if (last_digit (number o)) {
    position = intOf((number o)[match 0])
}

delete last_digit
print position

I'm looking for some function that could return, for example, int(4) when called on an object which is the 4th child in its immediate family.

Thanks!


dave.k - Wed Jun 04 15:14:40 EDT 2014

Re: DXL function for sibling position
llandale - Wed Jun 04 16:50:15 EDT 2014

Never heard of one and don't see one in the doors.exe.

I think you found two natural methods, except both really need to take into consideration whether deleted objects are showing or not.  That will certainly modify the "number" of an object.  If it were me though, I'd probably go with the "previous sibling(o)" command in a reverse recursive function; but then I often like going overboard.

-Louie

Re: DXL function for sibling position
dave.k - Wed Jun 04 17:19:05 EDT 2014

llandale - Wed Jun 04 16:50:15 EDT 2014

Never heard of one and don't see one in the doors.exe.

I think you found two natural methods, except both really need to take into consideration whether deleted objects are showing or not.  That will certainly modify the "number" of an object.  If it were me though, I'd probably go with the "previous sibling(o)" command in a reverse recursive function; but then I often like going overboard.

-Louie

So you'd prefer to see something like this then?

// find the position of an object relative to its siblings, given a starting position offset
int position(Object o, int starting_position) {
    Object previousSibling = previous sibling o
        if (null previousSibling) return starting_position // we're done
        else return position(previousSibling, starting_position+1) // keep backing up
}

// find the position of an object relative to its siblings
int position(Object o) {
        return position(o, 1) // return the 1-indexed position
}

Module m = current
showDeletedObjects(false)
print position (current Object)

Would this be more efficient, or is it just prettier? I'd think something like the string parsing method might be more efficient, since it's O(1), rather that something involving a loop like the above, where it's O(# objects in family).

How do you think the overhead of processing one object number string would relate to the overhead of processing multiple sibling object calls and recursion, etc. I'll be using this repeatedly.

 

Thanks for the quick reply!

- Dave

Re: DXL function for sibling position
Adamarla - Thu Jun 05 08:06:15 EDT 2014

llandale - Wed Jun 04 16:50:15 EDT 2014

Never heard of one and don't see one in the doors.exe.

I think you found two natural methods, except both really need to take into consideration whether deleted objects are showing or not.  That will certainly modify the "number" of an object.  If it were me though, I'd probably go with the "previous sibling(o)" command in a reverse recursive function; but then I often like going overboard.

-Louie

I disagree with using  "previous(sibling(Object))" due to the fact that it respects the display set and is therefore dependant on [in]visible deleted objects as well as filters. I would go with the "for Object in Object do" loop as it is most robust.

- Adam

Re: DXL function for sibling position
llandale - Fri Jun 06 16:24:45 EDT 2014

Adamarla - Thu Jun 05 08:06:15 EDT 2014

I disagree with using  "previous(sibling(Object))" due to the fact that it respects the display set and is therefore dependant on [in]visible deleted objects as well as filters. I would go with the "for Object in Object do" loop as it is most robust.

- Adam

Excellent observation.  Children of parent may look like this

int Position1(Object o)
{ int Count = 0
  if (null o) return(Count)
  Object oParent = parent(o), oChild
   if (null oParent) return(Count)
   for oChild in oParent do
  {  Count++
     if (oChild == o) then return(Count)
  }
  return(0)    // is this possible?
}

-Louie